package com.tianquan.exception;


import com.tianquan.log.Level;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class BaseException extends RuntimeException {

    private String msg;

    private int code;

    private String level;

    public BaseException(String level, String msg, int code) {
        this.level = level;
        this.msg = msg;
        this.code = code;
    }

    public static BaseException Error(Exception e, int code) {
        StackTraceElement[] stackTrace = e.getStackTrace();
        String msg = null;
        if (stackTrace.length == 0) msg = "Exception has no stack trace";
        StackTraceElement element = stackTrace[0];
        msg = "{ " + "ExceptionClass:" + element.getClassName() +
                ",ExceptionLineNumber:" + element.getLineNumber() +
                ",ExceptionType:" + e.getClass().getName() +
                ",ExceptionMessage:" + e.getMessage() + " }";
        return new BaseException(Level.ERROR.getValue(), msg, code);
    }

    public static BaseException WARN(String msg, int code) {
        return new BaseException(Level.WARN.getValue(), msg, code);
    }

    public static BaseException INFO(String msg, int code) {
        return new BaseException(Level.INFO.getValue(), msg, code);
    }


}
